﻿using System.Collections.Generic;
using System.Data;
using RpcHelper;
using RpcModel;
using RpcModel.Model;
using SqlExecHelper;
using Wedonek.RpcStore.Service.Model;

namespace Wedonek.RpcStore.Service.DAL
{
        internal class ServerDictateLimitDAL : SqlExecHelper.SqlBasicClass
        {
                public ServerDictateLimitDAL () : base ("ServerDictateLimit")
                {

                }
                public long Add (AddDictateLimit add)
                {
                        this.Insert (add, out long id);
                        return id;
                }

                public ServerDictateLimitData Get (long id)
                {
                        return this.GetRow<long, ServerDictateLimitData> ("Id", id);
                }
                public void Delete (long id)
                {
                        if (!this.Drop ("Id", id))
                        {
                                throw new ErrorException ("rpc.dictate.drop.error");
                        }
                }
                public void Set (long id, ServerDictateLimit limit)
                {
                        if (!base.Update (limit, "Id", id))
                        {
                                throw new ErrorException ("rpc.dictate.set.error");
                        }
                }
                public void Clear (long serverId)
                {
                        base.Drop ("ServerId", serverId);
                }
                public ServerDictateLimitData[] Query (long serverId, string dictate, IBasicPage paging, out long count)
                {
                        List<ISqlWhere> wheres = new List<ISqlWhere> () {
                                new SqlWhere("ServerId", SqlDbType.BigInt){Value=serverId}
                        };
                        if (!dictate.IsNull ())
                        {
                                wheres.Add (new SqlWhere ("Dictate", SqlDbType.VarChar, 50) { Value = dictate });
                        }
                        return base.Query<ServerDictateLimitData> ("Id desc", paging.Index, paging.Size, out count, wheres.ToArray ());
                }
        }
}
